<?php
namespace logic\admin\index;

use library\common\collections\ArrayUtils;
use model\AdminPower;
use think\Db;
/**
 * User: zj
 * Date: 2017/4/27
 * Time: 15:11
 * Progress: 95%
 */
class LAdminPower
{

    //权限菜单树列表
    public function index_data(){
        $model = new AdminPower();
        $where['flag_use'] = 1;
        $where['flag_delete'] = 0;
        $list = $model->where($where)->field("id,parent_id as p_id,name,domain,module,controller,function,flag_use,flag_delete,sort")->order('sort asc')->select();
        $list_tree = $this->tree_asc($list);
        return $list_tree;
    }

    protected function tree_asc($list, $p_id = 0) {
        $list_tree = array();
        foreach ($list as $menu) {
            if ($menu['p_id'] == $p_id) {

                if (!in_array($menu, $list_tree)) {
                    array_push($list_tree, $menu);
                }
                $list_son = $this->tree_asc($list, $menu['id']);
                if ($list_son) {
                    foreach ($list_son as $menu_son) {
                        array_push($list_tree, $menu_son);
                    }
                }
            }
        }
        return $list_tree;
    }


    public function edit_index($id){
        $model = new AdminPower();
        $data=$model->where(['id'=>$id])->find();
        return $data;

    }

    //修改菜单名称
    public function  edit_name($id,$param){
        $model = new AdminPower();
        $res = $model->where(['id'=>$id])->update($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }

    //删除权限
    public function delete_data($id){
        $model = new AdminPower();
        $data['flag_delete'] = 1;
        $res = $model->where(['id'=>$id])->update($data);
        if($res){
            return array(
                'success'=>true,
                'message'=>'删除成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }



}